import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

const router = new Router({
  mode: 'history',
  base: '/admin',
  routes: [
    {
      path: '/login',
      name: 'login',
      meta: {
        requireAuth: false,
      },
      component: () => import('@/pages/login/Login')
    },
    {
      path: '/',
      name: 'index',
      meta: {
        requireAuth: true,
      },
      components: {
        default: () => import('@/pages/Index'),
        nav: () => import('@/pages/Head/NavigationBar')
      },
      children: [
        {
          // 首页-首页
          path: '/',
          name: 'home',
          component: () => import('@/pages/Index/Home')
        }, {// 首页-欢迎面板
          path: '/welcome',
          name: 'welcome',
          component: () => import('@/pages/Index/Welcome')
        }, {
          // 首页-登录日志
          path: '/loginLog',
          name: 'loginLog',
          component: () => import('@/pages/Index/LoginLog')
        }, {// 首页-账户设置
          path: '/accountSet',
          name: 'accountSet',
          component: () => import('@/pages/Index/AccountSet')
        }, {
          // 首页-系统信息
          path: '/systemInfo',
          name: 'systemInfo',
          component: () => import('@/pages/Index/SystemInfo')
        },
        {
          // 商品-商品列表
          path: '/productList',
          name: 'productList',
          component: () => import('@/pages/product/ProductList')
        },
        {
          // 商品-商品添加
          path: '/addProduct',
          name: 'addProduct',
          component: () => import('@/pages/product/AddProduct')
        },
        {
          // 商品-导入商品
          path: '/importProducts',
          name: 'importProducts',
          component: () => import('@/pages/product/importProducts')
        },
        {
          // 商品-商品审核列表
          path: '/productAuditList',
          name: 'productAuditList',
          component: () => import('@/pages/product/ProductAuditList')
        },
        {
          // 商品-待审核商品列表
          path: '/toBeAuditProductList',
          name: 'toBeAuditProductList',
          component: () => import('@/pages/product/ToBeAuditProductList')
        },
        {
          // 商品-商品审核记录
          path: '/productAuditRecord',
          name: 'productAuditRecord',
          component: () => import('@/pages/product/ProductAuditRecord')
        },
        {
          // 商品-一级分类
          path: '/firstClassification',
          name: 'firstClassification',
          component: () => import('@/pages/product/FirstClassification')
        },
        {
          // 商品-二级分类
          path: '/secondClassification',
          name: 'secondClassification',
          component: () => import('@/pages/product/SecondClassification')
        },
        {
          // 商品-三级分类
          path: '/thirdClassification',
          name: 'thirdClassification',
          component: () => import('@/pages/product/ThirdClassification')
        },
        {
          // 订单-订单列表
          path: '/orderList',
          name: 'orderList',
          component: () => import('@/pages/Order/OrderList')
        }, {
          // 用户-供应商审核列表
          path: '/supplierAuditList',
          name: 'supplierAuditList',
          component: () => import('@/pages/Role/SupplierAuditList')
        }, {
          // 用户-供应商列表
          path: '/supplierList',
          name: 'supplierList',
          component: () => import('@/pages/Role/SupplierList')
        }, {
          // 用户-采购商审核列表
          path: '/merchantAuditList',
          name: 'merchantAuditList',
          component: () => import('@/pages/Role/MerchantAuditList')
        }, {
          // 用户-登陆日志
          path: '/Role/log',
          name: '/Role/log',
          component: () => import('@/pages/Role/components/Log')
        }, {
          // 用户-采购商列表
          path: '/merchantList',
          name: 'merchantList',
          component: () => import('@/pages/Role/MerchantList')
        }, {
          //用户-商户服务关系
          path: '/businessServiceRelationship',
          name: 'businessServiceRelationship',
          component: () => import('@/pages/Role/BusinessServiceRelationship')
        }, {
          //用户-采购商子账户 -- 非右侧菜单
          path: '/buyerSubAccount',
          name: 'buyerSubAccount',
          component: () => import('@/pages/Role/components/BuyerSubAccount')
        }, {
          //用户-电子合同管理
          path: '/electronicContract',
          name: '/electronicContract',
          component: () => import('@/pages/Role/ElectronicContract')
        }, {
          // 财务-财务统计
          path: '/financialStatistics',
          name: 'financialStatistics',
          component: () => import('@/pages/Finance/FinancialStatistics')
        }, {
          // 财务-供应商资金账户
          path: '/supplierCapitalAccount',
          name: 'supplierCapitalAccount',
          component: () => import('@/pages/Finance/SupplierCapitalAccount')
        }, {
          // 财务-采购商资金账户
          path: '/merchantCapitalAccount',
          name: 'merchantCapitalAccount',
          component: () => import('@/pages/Finance/MerchantCapitalAccount')
        }, {
          // 财务-结算流水账户
          path: '/supplierCapitalAccount/AccountModel',
          name: '/supplierCapitalAccount/AccountModel',
          component: () => import('@/pages/Finance/components/AccountModel')
        }, {
          // 财务-供应商应收未收统计
          path: '/supplierStatistics',
          name: 'supplierStatistics',
          component: () => import('@/pages/Finance/SupplierStatistics')
        }, {
          // 财务-采购商应收未收统计
          path: '/merchantStatistics',
          name: 'merchantStatistics',
          component: () => import('@/pages/Finance/MerchantStatistics')
        }, {
          // 财务-充值核对
          path: '/rechargeCheck',
          name: 'rechargeCheck',
          component: () => import('@/pages/Finance/RechargeCheck')
        }, {
          // 财务-充值对账
          path: '/rechargeReconciliation',
          name: 'rechargeReconciliation',
          component: () => import('@/pages/Finance/RechargeCheck')
        }, {
          // 财务-财务规则设置
          path: '/accountRuleSet',
          name: 'accountRuleSet',
          component: () => import('@/pages/Finance/AccountRuleSet')
        }, {
          // 财务-采购商账期方案管理
          path: '/merchantAPManagement',
          name: 'merchantAPManagement',
          component: () => import('@/pages/Finance/MerchantAPManagement')
        }, {
          // 财务-供应商转出方案管理
          path: '/supplierTPManagement',
          name: 'supplierTPManagement',
          component: () => import('@/pages/Finance/SupplierTPManagement')
        }, {
          // 财务-返利方案管理
          path: '/rebateManagement',
          name: 'rebateManagement',
          component: () => import('@/pages/Finance/RebateManagement')
        }, {
          // 单独添加页面 余额页面
          path: '/balanceFlow',
          name: 'balanceFlow',
          component: () => import('@/pages/Finance/components/balanceFlow')
        }, {
          // 单独添加页面 信用页面
          path: '/xinBalance',
          name: 'xinBalance',
          component: () => import('@/pages/Finance/components/XinBalance')
        }, {
          // 单独添加页面 鲜丰惠
          path: '/xfhBalance',
          name: 'xfhBalance',
          component: () => import('@/pages/Finance/components/XfhBalance')
        }, {
          // 保理-商户转出审核
          path: '/businessTransferAudit',
          name: 'businessTransferAudit',
          component: () => import('@/pages/Factoring/BusinessTransferAudit')
        }, {
          // 保理-采购商信用账单
          path: '/merchantCreditBill',
          name: 'merchantCreditBill',
          component: () => import('@/pages/Factoring/MerchantCreditBill')
        }, {
          // 保理-账单还款审核列表
          path: '/billRepaymentAuditList',
          name: 'billRepaymentAuditList',
          component: () => import('@/pages/Factoring/BillRepaymentAuditList')
        },

        // {
        //   // 保理-账单还款对账列表
        //   path: '/billRepaymentRList',
        //   name: 'billRepaymentRList',
        //   component: () => import('@/pages/Factoring/BillRepaymentRList')
        // }, {
        //   // 保理-违约账单列表
        //   path: '/defaultingBillList',
        //   name: 'defaultingBillList',
        //   component: () => import('@/pages/Factoring/DefaultingBillList')
        // },

        {
          // 保理-订单信息费结算
          path: '/orderInfoFeeSettlement',
          name: 'orderInfoFeeSettlement',
          component: () => import('@/pages/Factoring/OrderInfoFeeSettlement')
        }, {
          // 保理-供应商结算账户流水
          path: '/supplierSettlementAcountFlow',
          name: 'supplierSettlementAcountFlow',
          component: () => import('@/pages/Factoring/SupplierSettlementAcountFlow')
        }, {
          // 保理-提前转出结算
          path: '/advanceTransferSettlement',
          name: 'advanceTransferSettlement',
          component: () => import('@/pages/Factoring/AdvanceTransferSettlement')
        }, {
          // 保理-供应商余额账户流水
          path: '/supplierBalanceAccountFlow',
          name: 'supplierBalanceAccountFlow',
          component: () => import('@/pages/Factoring/supplierBalanceAccountFlow')
        }, {
          // 保理-集鲜丰账户流水
          path: '/freshHuiAccountFlow',
          name: 'freshHuiAccountFlow',
          component: () => import('@/pages/Factoring/FreshHuiAccountFlow')
        }, {
          // 保理-保理账户流水
          path: '/factoringAccountFlow',
          name: 'factoringAccountFlow',
          component: () => import('@/pages/Factoring/FactoringAccountFlow')
        }, {
          // 保理-商户提现审核
          path: '/businessWithdrawCashAudit',
          name: 'businessWithdrawCashAudit',
          component: () => import('@/pages/Factoring/BusinessWithdrawCashAudit')
        }, {
          // 保理-提现发起列表
          path: '/withdrawCashLaunchList',
          name: 'withdrawCashLaunchList',
          component: () => import('@/pages/Factoring/WithdrawCashLaunchList')
        }, {
        //   // 保理-提现对账
        //   path: '/withdrawCashReconciliations',
        //   name: 'withdrawCashReconciliations',
        //   component: () => import('@/pages/Factoring/WithdrawCashReconciliations')
        // }, {
          // 保理-次月额度发放
          path: '/nextMonthQuotaGrant',
          name: 'nextMonthQuotaGrant',
          component: () => import('@/pages/Factoring/NextMonthQuotaGrant')
        }, {
        // 保理-固定额度发放
          path: '/fixedQuotaApplication',
          name: 'fixedQuotaApplication',
          component: () => import('@/pages/Factoring/FixedQuotaApplication')
        }, {
        // 保理-临时额度发放
          path: '/emporaryQuotaApplication',
          name: 'emporaryQuotaApplication',
          component: () => import('@/pages/Factoring/EmporaryQuotaApplication')
        }, {
        // 保理-星级管理
          path: '/starManagement',
          name: 'starManagement',
          component: () => import('@/pages/Factoring/StartManagement')
        }, {
          // 运营-QA列表
          path: '/QAList',
          name: 'QAList',
          component: () => import('@/pages/Operation/QAList')
        }, {
          // 运营-公告消息
          path: '/noticeNews',
          name: 'noticeNews',
          component: () => import('@/pages/Operation/NoticeNews')
        }, {
          // 运营-广告列表
          path: '/advertisingList',
          name: 'advertisingList',
          component: () => import('@/pages/Operation/AdvertisingList')
        }, {
          // 运营-意见反馈
          path: '/feedback',
          name: 'feedback',
          component: () => import('@/pages/Operation/Feedback')
        },{
          // 架构-子公司列表
          path: '/subsidiaryList',
          name: 'subsidiaryList',
          component: () => import('@/pages/Architecture/SubsidiaryList')
        }, {
          // 架构-渠道商列表
          path: '/channelList',
          name: 'channelList',
          component: () => import('@/pages/Architecture/ChannelList')
        }, {
          // 架构-业务员列表
          path: '/salesmanList',
          name: 'salesmanList',
          component: () => import('@/pages/Architecture/SalesmanList')
        }, {
          // 设置-参数设置
          path: '/paramSet',
          name: 'paramSet',
          component: () => import('@/pages/Setting/ParamSet')
        }, {
          // 设置-用户协议
          path: '/userProtocol',
          name: 'userProtocol',
          component: () => import('@/pages/Setting/UserProtocol')
        }, {
          // 设置-子公司运费设置
          path: '/subsidiaryShipSetting',
          name: 'subsidiaryShipSetting',
          component: () => import('@/pages/Setting/SubsidiaryShipSetting')
        }, {
          // 设置-子公司专属保护
          path: '/subsidiaryProtection',
          name: 'subsidiaryProtection',
          component: () => import('@/pages/Setting/SubsidiaryProtection')
        }, {
          // 设置-运费设置
          path: '/shipSetting',
          name: 'shipSetting',
          component: () => import('@/pages/Setting/ShipSetting')
        }, {
          // 权限-部门管理
          path: '/departmentManagement',
          name: 'departmentManagement',
          component: () => import('@/pages/auth/DepartmentManagement')
        },{
          // 权限-成员管理
          path: '/memberManagement',
          name: 'memberManagement',
          component: () => import('@/pages/auth/MemberManagement')
        // },{
        //   // 权限-数据库管理
        //   path: '/daabaseManagement',
        //   name: 'daabaseManagement',
        //   component: () => import('@/pages/Setting/UserProtocol')
        // },{
        //   // 消息-消息展示
        //   path: '/messageDisplay',
        //   name: 'messageDisplay',
        //   component: () => import('@/pages/Setting/UserProtocol')
        }, {
          // 按钮权限
          path: '/menuPermission',
          name: "menuPermission",
          component: () => import('@/pages/Permission/MenuPermission')
        },  {
          // 404
          path: "/404",
          name: "404",
          meta: {
            requireAuth: true,
          },
          component: () => import('@/pages/404/404')
        }, {
          // 500
          path: "/500",
          name: "500",
          meta: {
            requireAuth: true,
          },
          component: () => import('@/pages/500/500')
        }]
    },
    {
      // 不存在路由
      path: "*",
      name: "error",
      redirect: "/404"
    }
  ]
})

// 游客页面
router.beforeEach((to, from, next) => {
  console.log(to.matched.some(record => record.meta.requiresAuth))
  if(to.name === 'xinBalance' || to.name === 'balanceFlow' || to.name === 'xfhBalance') {
    // this.$router.push({name: 'merchantCapitalAccount'});
    // next()
    if(!to.params.userId) {
      next('merchantCapitalAccount')
    }
  }
  if (to.meta.requiresAuth) {
    window.localStorage.adminLogin ? next() : next({
      name: 'login'
    })
  } else next()
})

export default router
